package com.caishi.lkx.exam.dto;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.caishi.lkx.exam.ienum.type.QuestionType;

import com.caishi.lkx.exam.model.QuestionModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.caishi.lkx.exam.util.QuestionUtil.*;

@Getter
@Setter
@Builder
public class QuestionSimpleDto {
    private String id;

    @Schema(title = "题型")
    private QuestionType firstType;

    @Schema(title = "小题型id")
    private String typeId;

    @Schema(title = "题干")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> stem;

    @Schema(title = "选项")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> quOption;

    @Schema(title = "答案")
    private Object answer;

    @Schema(title = "答案解析")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> answerDesc;

    private List<QuestionSimpleDto> children;


    public List<Map<String, Object>> getStem() {
        return sensitiveContent(this.stem);
    }


    public List<Map<String, Object>> getQuOption() {
        return sensitiveContent(this.quOption);
    }
    public List<Map<String, Object>> getAnswerDesc() {
        return sensitiveContent(this.answerDesc);
    }

    public static QuestionSimpleDto modelTrans(QuestionModel model) {
        return QuestionSimpleDto.builder()
                .id(model.getId())
                .answerDesc(model.getAnswerDesc())
                .firstType(model.getFirstType())
                .quOption(model.getQuOption())
                .stem(model.getStem())
                .answer(model.getAnswer())
                .typeId(model.getTypeId())
                .children(CollUtil.isEmpty(model.getChildren()) ? null :  model.getChildren().stream().map(QuestionSimpleDto::modelTrans).collect(Collectors.toList()))
                .build();
    }
}
